#include "u_Task.h"
#include "bsp_Task.h"
#include "bsp_port.h"
#include "bsp_Timer.h"
#include "User_App.h"
#include "menu.h"

#if 1
extern info_t info;
extern bsp_TIM_t Ti_Add_Water;
extern Menu_t MenuVIII_Automatic_Add_Water[];
extern Menu_t MenuVIII_Change_Water[];
extern Menu_t MenuVIII_Lamp_2[];
extern Menu_t MenuVIII_Circulate_Pump[];

static u_Task_e u_Add_Water_Init_Start(void);
static u_Task_e u_Add_Water_Init_End(void);
static u_Task_e u_Add_Water_Init_Pause(void);
static u_Task_e u_Add_Water_Init_Renew(void);
static u_Task_e u_Add_Water_Bide_Start(void);
static u_Task_e u_Add_Water_Bide_End(void);
static u_Task_e u_Add_Water_Bide_Pause(void);
static u_Task_e u_Add_Water_Bide_Renew(void);
static u_Task_e u_Add_Water_Stop_Event(void);
static u_Task_e u_Add_Water_Err_Event(void);
static u_Task_e u_Add_Water_Waterline_Start(void);
static u_Task_e u_Add_Water_Waterline_End(void);
static u_Task_e u_Add_Water_Waterline_Pause(void);
static u_Task_e u_Add_Water_Waterline_Renew(void);
static u_Task_e u_Add_Water_PumpOpen_Start(void);
static u_Task_e u_Add_Water_PumpOpen_End(void);
static u_Task_e u_Add_Water_PumpOpen_Pause(void);
static u_Task_e u_Add_Water_PumpOpen_Renew(void);

static u_Task_e u_Change_Water_Init_Start(void);
static u_Task_e u_Change_Water_Init_End(void);
static u_Task_e u_Change_Water_Init_Pause(void);
static u_Task_e u_Change_Water_Init_Renew(void);
static u_Task_e u_Change_Water_Wait_Drop_Start(void);
static u_Task_e u_Change_Water_Wait_Drop_End(void);
static u_Task_e u_Change_Water_Wait_Drop_Pause(void);
static u_Task_e u_Change_Water_Wait_Drop_Renew(void);
static u_Task_e u_Change_Water_Excrete_Start(void);
static u_Task_e u_Change_Water_Excrete_End(void);
static u_Task_e u_Change_Water_Excrete_Pause(void);
static u_Task_e u_Change_Water_Excrete_Renew(void);
static u_Task_e u_Change_Water_Add_Start(void);
static u_Task_e u_Change_Water_Add_End(void);
static u_Task_e u_Change_Water_Add_Pause(void);
static u_Task_e u_Change_Water_Add_Renew(void);
static u_Task_e u_Change_Water_Trigger_Start(void);
static u_Task_e u_Change_Water_Trigger_End(void);
static u_Task_e u_Change_Water_Trigger_Pause(void);
static u_Task_e u_Change_Water_Trigger_Renew(void);
static u_Task_e u_Change_Water_Circulate_Start(void);
static u_Task_e u_Change_Water_Circulate_End(void);
static u_Task_e u_Change_Water_Circulate_Pause(void);
static u_Task_e u_Change_Water_Circulate_Renew(void);
static u_Task_e u_Change_Water_Stop_Event(void);
static u_Task_e u_Change_Water_Err_Event(void);
#endif
// 自动补水
u_Task_t Add_Water = {
    .t_State = t_STOP,
    .t_Num = 3,
    .t_TaskNode[0] = {
        // 补水初始化 打开底滤照明、延时关闭主循环泵
        .ta_TimeOut = 6000,
        .u_Task_Handle[U_START] = u_Add_Water_Init_Start,
        .u_Task_Handle[U_END] = u_Add_Water_Init_End,
        .u_Task_Handle[U_PAUSE] = u_Add_Water_Init_Pause,
        .u_Task_Handle[U_RENEW] = u_Add_Water_Init_Renew,
    },
    .t_TaskNode[1] = {
        // 延时等待水位回落滤材仓，然后打开补水泵
        .ta_TimeOut = 35000,
        .u_Task_Handle[U_START] = u_Add_Water_Bide_Start,
        .u_Task_Handle[U_END] = u_Add_Water_Bide_End,
        .u_Task_Handle[U_PAUSE] = u_Add_Water_Bide_Pause,
        .u_Task_Handle[U_RENEW] = u_Add_Water_Bide_Renew,
    },
    .t_TaskNode[2] = {
        // 检测是否到达补水水位。如果超时，结束任务
        .ta_TimeOut = 130000,
        .u_Task_Handle[U_START] = u_Add_Water_Waterline_Start,
        .u_Task_Handle[U_END] = u_Add_Water_Waterline_End,
        .u_Task_Handle[U_PAUSE] = u_Add_Water_Waterline_Pause,
        .u_Task_Handle[U_RENEW] = u_Add_Water_Waterline_Renew,
    },
    .t_TaskNode[3] = {
        // 任务完成，延时启动主循环水泵
        .ta_TimeOut = 10000,
        .u_Task_Handle[U_START] = u_Add_Water_PumpOpen_Start,
        .u_Task_Handle[U_END] = u_Add_Water_PumpOpen_End,
        .u_Task_Handle[U_PAUSE] = u_Add_Water_PumpOpen_Pause,
        .u_Task_Handle[U_RENEW] = u_Add_Water_PumpOpen_Renew,
    },
    .t_StopEvent = u_Add_Water_Stop_Event,
    .t_ErrEvent = u_Add_Water_Err_Event,
};

static u_Task_e u_Add_Water_Init_Start(void)
{
    bsp_Ctrl_Down_Lamp(ON_s);
    return t_OK;
}
static u_Task_e u_Add_Water_Init_End(void)
{
    if (bsp_Ti_Get_mDelay() == 0)
    {
        bsp_Ctrl_Circulate_Pump(OFF_s); // 关闭主循环泵
        return t_OK;
    }
    return t_NOK;
}
static u_Task_e u_Add_Water_Init_Pause(void)
{
    return t_OK;
}
static u_Task_e u_Add_Water_Init_Renew(void)
{
    return t_OK;
}

static u_Task_e u_Add_Water_Bide_Start(void)
{
    // 设置延时时间
    bsp_Ti_Set_mDelay(20000);
    return t_OK;
}
static u_Task_e u_Add_Water_Bide_End(void)
{
    if (bsp_Ti_Get_mDelay() == 0)
    {
        if (bsp_Get_Waterline() == 0) // 如果水位回落后未触发补水水位，说明缺水
        {
            bsp_Ctrl_Supply_Pump(ON_s);
            return t_OK;
        }
        else
        {
            bsp_Ctrl_Supply_Pump(OFF_s);
            return t_OK;
        }
    }
    return t_NOK;
}
static u_Task_e u_Add_Water_Bide_Pause(void)
{
    return t_OK;
}
static u_Task_e u_Add_Water_Bide_Renew(void)
{
    return t_OK;
}

static u_Task_e u_Add_Water_Waterline_Start(void)
{
    return t_OK;
}
static u_Task_e u_Add_Water_Waterline_End(void)
{
    if (bsp_Get_Waterline() == 1) // 如果水位触发补水水位，说明补水完成
    {
        bsp_Ctrl_Supply_Pump(OFF_s); // 关闭补水水泵
        return t_OK;
    }
    return t_NOK;
}
static u_Task_e u_Add_Water_Waterline_Pause(void)
{
    return t_OK;
}
static u_Task_e u_Add_Water_Waterline_Renew(void)
{
    return t_OK;
}

static u_Task_e u_Add_Water_PumpOpen_Start(void)
{
    // 设置延时时间
    bsp_Ti_Set_mDelay(5000);
    return t_OK;
}
static u_Task_e u_Add_Water_PumpOpen_End(void)
{
    if (bsp_Ti_Get_mDelay() == 0)
    {
        bsp_Ctrl_Circulate_Pump(ON_s); // 打开主循环泵
        return t_OK;
    }
    return t_NOK;
}
static u_Task_e u_Add_Water_PumpOpen_Pause(void)
{
    return t_OK;
}
static u_Task_e u_Add_Water_PumpOpen_Renew(void)
{
    return t_OK;
}

static u_Task_e u_Add_Water_Stop_Event(void)
{
    bsp_Ctrl_Supply_Pump(OFF_s);          // 防止中途结束“自动补水”任务 补水电机还在运行
    bsp_Ctrl_Down_Lamp(OFF_s);            // 关闭底滤照明
    MenuVIII_Lamp_2[1].Name = "状态[关]"; // 同步底滤照明状态
    info.Manual_Model_t.Lamp2_t.State = 0;

    return t_OK;
}
static u_Task_e u_Add_Water_Err_Event(void)
{
    bsp_Ctrl_Supply_Pump(OFF_s);          // 防止补水超时了 补水电机还在运行
    bsp_Ctrl_Down_Lamp(OFF_s);            // 关闭底滤照明
    MenuVIII_Lamp_2[1].Name = "状态[关]"; // 同步底滤照明状态
    info.Manual_Model_t.Lamp2_t.State = 0;

    // 关闭自动补水
    MenuVIII_Automatic_Add_Water[1].Name = "状态[关]";
    info.Automatic_Model_t.Add_Water_t.State = 0;
    Ti_obj_Set_Stop(&Ti_Add_Water);
    // 打开主循环泵 补水桶没水的情况下，优先保证主缸水循环
    bsp_Ctrl_Circulate_Pump(ON_s);
    MenuVIII_Circulate_Pump[1].Name = "状态[开]";
    info.Manual_Model_t.Circulate_Pump_t.State = 0;

    return t_OK;
}
// 自动换水
u_Task_t Change_Water = {
    .t_State = t_STOP,
    .t_Num = 5,
    .t_TaskNode[0] = {
        // 开启底部灯光
        // 延时6s
        // 关闭主循环泵
        .ta_TimeOut = 10000,
        .u_Task_Handle[U_START] = u_Change_Water_Init_Start,
        .u_Task_Handle[U_END] = u_Change_Water_Init_End,
        .u_Task_Handle[U_PAUSE] = u_Change_Water_Init_Pause,
        .u_Task_Handle[U_RENEW] = u_Change_Water_Init_Renew,
    },
    .t_TaskNode[1] = {
        // 设置延时时间
        // 等待水位回落入底缸
        .ta_TimeOut = 35000,
        .u_Task_Handle[U_START] = u_Change_Water_Wait_Drop_Start,
        .u_Task_Handle[U_END] = u_Change_Water_Wait_Drop_End,
        .u_Task_Handle[U_PAUSE] = u_Change_Water_Wait_Drop_Pause,
        .u_Task_Handle[U_RENEW] = u_Change_Water_Wait_Drop_Renew,
    },
    .t_TaskNode[2] = {
        // 设置排水时长
        // 开启排水泵
        // 等待排水结束
        // 关闭排水
        .ta_TimeOut = 130000,
        .u_Task_Handle[U_START] = u_Change_Water_Excrete_Start,
        .u_Task_Handle[U_END] = u_Change_Water_Excrete_End,
        .u_Task_Handle[U_PAUSE] = u_Change_Water_Excrete_Pause,
        .u_Task_Handle[U_RENEW] = u_Change_Water_Excrete_Renew,
    },
    .t_TaskNode[3] = {
        // 延时6s
        // 开启补水泵
        .ta_TimeOut = 10000,
        .u_Task_Handle[U_START] = u_Change_Water_Add_Start,
        .u_Task_Handle[U_END] = u_Change_Water_Add_End,
        .u_Task_Handle[U_PAUSE] = u_Change_Water_Add_Pause,
        .u_Task_Handle[U_RENEW] = u_Change_Water_Add_Renew,
    },
    .t_TaskNode[4] = {
        // 等待到达补水水位
        // 关闭补水泵
        .ta_TimeOut = 180000,
        .u_Task_Handle[U_START] = u_Change_Water_Trigger_Start,
        .u_Task_Handle[U_END] = u_Change_Water_Trigger_End,
        .u_Task_Handle[U_PAUSE] = u_Change_Water_Trigger_Pause,
        .u_Task_Handle[U_RENEW] = u_Change_Water_Trigger_Renew,
    },
    .t_TaskNode[5] = {
        // 延时6s
        // 开启主循环泵
        .ta_TimeOut = 10000,
        .u_Task_Handle[U_START] = u_Change_Water_Circulate_Start,
        .u_Task_Handle[U_END] = u_Change_Water_Circulate_End,
        .u_Task_Handle[U_PAUSE] = u_Change_Water_Circulate_Pause,
        .u_Task_Handle[U_RENEW] = u_Change_Water_Circulate_Renew,
    },
    // 结束任务
    .t_StopEvent = u_Change_Water_Stop_Event,
    .t_ErrEvent = u_Change_Water_Err_Event,
};

static u_Task_e u_Change_Water_Init_Start(void)
{
    bsp_Ctrl_Down_Lamp(ON_s);
    bsp_Ti_Set_mDelay(6000);
    return t_OK;
}
static u_Task_e u_Change_Water_Init_End(void)
{
    if (bsp_Ti_Get_mDelay() == 0)
    {
        bsp_Ctrl_Circulate_Pump(OFF_s); // 关闭主循环泵
        return t_OK;
    }
    return t_NOK;
}
static u_Task_e u_Change_Water_Init_Pause(void)
{
    return t_OK;
}
static u_Task_e u_Change_Water_Init_Renew(void)
{
    return t_OK;
}

static u_Task_e u_Change_Water_Wait_Drop_Start(void)
{
    bsp_Ti_Set_mDelay(20000);
    return t_OK;
}
static u_Task_e u_Change_Water_Wait_Drop_End(void)
{
    if (bsp_Ti_Get_mDelay() == 0)
    {
        return t_OK;
    }
    return t_NOK;
}
static u_Task_e u_Change_Water_Wait_Drop_Pause(void)
{
    return t_OK;
}
static u_Task_e u_Change_Water_Wait_Drop_Renew(void)
{
    return t_OK;
}

static u_Task_e u_Change_Water_Excrete_Start(void)
{
    bsp_Ti_Set_mDelay(120000);
    bsp_Ctrl_Expel_Pump(ON_s);

    return t_OK;
}
static u_Task_e u_Change_Water_Excrete_End(void)
{
    if (bsp_Ti_Get_mDelay() == 0)
    {
        bsp_Ctrl_Expel_Pump(OFF_s);
        return t_OK;
    }
    return t_NOK;
}
static u_Task_e u_Change_Water_Excrete_Pause(void)
{
    return t_OK;
}
static u_Task_e u_Change_Water_Excrete_Renew(void)
{
    return t_OK;
}

static u_Task_e u_Change_Water_Add_Start(void)
{
    bsp_Ti_Set_mDelay(6000);
    return t_OK;
}
static u_Task_e u_Change_Water_Add_End(void)
{
    if (bsp_Ti_Get_mDelay() == 0)
    {
        bsp_Ctrl_Supply_Pump(ON_s);
        return t_OK;
    }
    return t_NOK;
}
static u_Task_e u_Change_Water_Add_Pause(void)
{
    return t_OK;
}
static u_Task_e u_Change_Water_Add_Renew(void)
{
    return t_OK;
}

static u_Task_e u_Change_Water_Trigger_Start(void)
{
    return t_OK;
}
static u_Task_e u_Change_Water_Trigger_End(void)
{
    if (bsp_Get_Waterline() == 1) // 如果水位触发补水水位，说明补水完成
    {
        bsp_Ctrl_Supply_Pump(OFF_s); // 关闭补水水泵
        return t_OK;
    }
    return t_NOK;
}
static u_Task_e u_Change_Water_Trigger_Pause(void)
{
    return t_OK;
}
static u_Task_e u_Change_Water_Trigger_Renew(void)
{
    return t_OK;
}

static u_Task_e u_Change_Water_Circulate_Start(void)
{
    bsp_Ti_Set_mDelay(6000);
    return t_OK;
}
static u_Task_e u_Change_Water_Circulate_End(void)
{
    if (bsp_Ti_Get_mDelay() == 0)
    {
        bsp_Ctrl_Circulate_Pump(ON_s); // 打开主循环泵
        MenuVIII_Circulate_Pump[1].Name = "状态[开]";
        return t_OK;
    }
    return t_NOK;
}
static u_Task_e u_Change_Water_Circulate_Pause(void)
{
    return t_OK;
}
static u_Task_e u_Change_Water_Circulate_Renew(void)
{
    return t_OK;
}

static u_Task_e u_Change_Water_Stop_Event(void)
{
    bsp_Ctrl_Expel_Pump(OFF_s);           // 防止中途结束“自动排水”任务 排水电机还在运行
    bsp_Ctrl_Supply_Pump(OFF_s);          // 防止中途结束“自动排水”任务 补水电机还在运行
    bsp_Ctrl_Down_Lamp(OFF_s);            // 关闭底滤照明
    MenuVIII_Lamp_2[1].Name = "状态[关]"; // 同步底滤照明状态
    info.Manual_Model_t.Lamp2_t.State = 0;
    MenuVIII_Change_Water[1].Name = "状态[关]";

    return t_OK;
}
static u_Task_e u_Change_Water_Err_Event(void)
{
    bsp_Ctrl_Expel_Pump(OFF_s);           // 防止中途结束“自动排水”任务 排水电机还在运行
    bsp_Ctrl_Supply_Pump(OFF_s);          // 防止中途结束“自动排水”任务 补水电机还在运行
    bsp_Ctrl_Down_Lamp(OFF_s);            // 关闭底滤照明
    MenuVIII_Lamp_2[1].Name = "状态[关]"; // 同步底滤照明状态
    info.Manual_Model_t.Lamp2_t.State = 0;

    // 关闭自动换水
    MenuVIII_Change_Water[1].Name = "状态[关]";
    info.Automatic_Model_t.Change_Water_t.State = 0;

    // 打开主循环泵 补水桶没水的情况下，优先保证主缸水循环
    bsp_Ctrl_Circulate_Pump(ON_s);
    MenuVIII_Circulate_Pump[1].Name = "状态[开]";
    info.Manual_Model_t.Circulate_Pump_t.State = 0;

    return t_OK;
}

uint8_t u_Task_Switch;
/*  @brief  任务运行调试
    @return 无
    @property 无
*/
void u_Task_Debug(void)
{
    bsp_Task_Obj_New(&Add_Water);

    while (1)
    {
        bsp_Task_Handle();

        switch (u_Task_Switch)
        {
        case 1:
            bsp_Task_Obj_Set_Run(&Add_Water);
            break;
        case 2:
            bsp_Task_Obj_Set_Pause(&Add_Water);
            break;
        case 3:
            bsp_Task_Obj_Set_Renew(&Add_Water);
            break;
        case 4:
            bsp_Task_Obj_Set_Stop(&Add_Water);
            break;
        default:
            break;
        }
        u_Task_Switch = 0;
    }
}
